package cn.wbu.zyh.service.impl;

import cn.wbu.zyh.dto.AnnotationPageQueryDTO;
import cn.wbu.zyh.dto.ArticleDTO;
import cn.wbu.zyh.entity.Annotation;
import cn.wbu.zyh.entity.Article;
import cn.wbu.zyh.entity.ArticleTag;
import cn.wbu.zyh.mapper.AnnotationMapper;
import cn.wbu.zyh.result.PageResult;
import cn.wbu.zyh.service.AnnotationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AnnotationServiceImpl implements AnnotationService {
    @Autowired
    private AnnotationMapper annotationMapper;
    @Override
    public PageResult pageQueryByNoteId(AnnotationPageQueryDTO annotationPageQueryDTO) {
        PageHelper.startPage(annotationPageQueryDTO.getPage(), annotationPageQueryDTO.getPageSize());
        Page<Annotation> page = annotationMapper.pageQueryByNoteId(annotationPageQueryDTO);
        long total = page.getTotal();
        List<Annotation> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void insertAnnotationsBatch(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO, article);
        List<Annotation> annotations = articleDTO.getAnnotations();
        if (annotations != null && !annotations.isEmpty()) {
            annotationMapper.insertAnnotationsBatch(annotations);
        }
    }
}
